#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import Optional, TypeVar, Generic

from pydantic.generics import GenericModel

BASIC_EXCEPTION_MESSAGE = {
    1000: "成功",
    1001: "失败",
    10000: "未登录",
    10001: "登录失败",
    10002: "账号或密码不正确",
    10003: "令牌失效",
    11001: "非法参数",
    11002: "数据异常",
    12000: "保存失败",
    12001: "更新失败",
    12002: "删除失败",
    12003: "获取失败"
}

"""
系统编码：3位 模块 3位 异常码4位
AAABBBCCCC
"""
EXCEPTION_MESSAGE = {
    # user_setting 100 100
    1001001000: "用户设置不存在",
    # strategy 100 101
    1001011000: "策略不存在",
    1001011001: "上级分类不存在",
    1001011002: "不能在该分类或策略下添加分类",
    1001011003: "分类不存在",
    1001011004: "当前分类下有其他分类或策略",
    1001011005: "不能在该分类下添加策略",
    1001011006: "没有权限",
    # instrument 100 102
    1001021000: "商品不存在",
    # instrument 100 103
    1001031000: "K线不存在",
    # favorite 100 104
    1001041000: "收藏不存在",
    # position 100 105
    1001051000: "仓位不存在",
    1001051001: "仓位重复",

    # trader 100 106
    1001061000: "回测执行失败：%s"
}


class ExceptionMessage:
    def __init__(self):
        self.message = {}
        self.initMessage()

    def initMessage(self):
        for key, value in BASIC_EXCEPTION_MESSAGE.items():
            self.message[str(key)] = value
        for key, value in EXCEPTION_MESSAGE.items():
            self.message[str(key)] = value

    def get(self, code):
        return self.message[code]


class BasicExceptionCode:
    # 成功
    SUCCESS = "1000"

    # 失败
    FAIL = "1001"

    ##############登录####################

    # 未登录
    UNLOGINED = "10000"

    # 登录失败
    LOGIN_FAILED = "10001"

    # 账号或密码不正确
    USERNAME_PASSWORD_INCORRECT = "10002"

    # 令牌失效
    TOKEN_EXPIRED = "10003"

    ###############数据###################

    # 非法参数
    ILLEGAL_PARAMETER = "11001"

    # 数据异常
    DATA_ERROR = "11001"

    ##############CRUD操作####################

    # 保存失败
    SAVE_ERROR = "12000"

    # 更新失败
    UPDATE_ERROR = "12001"

    # 删除失败
    DELETE_ERROR = "12002"

    # 获取失败
    GET_ERROR = "12003"


class ExceptionCode:
    USER_SETTING_DOES_NOT_EXIST = "1001001000"

    # strategy 100 101
    STRATEGY_DOES_NOT_EXIST = "1001011000"
    PARENT_DOES_NOT_EXIST = "1001011001"
    CAN_NOT_ADD_CATEGORY = "1001011002"
    CATEGORY_DOES_NOT_EXIST = "1001011003"
    CATEGORY_HAS_CHILDREN = "1001011004"
    CAN_NOT_ADD_STRATEGY_IN_CATEGORY = "1001011005"
    EDIT_STRATEGY_WITH_NONE_PRIVILEGE = "1001011006"

    # instrument 100 102
    INSTRUMENT_DOES_NOT_EXIST = "1001021000"

    # bar 100 103
    BAR_DOES_NOT_EXIST = "1001031000"

    # favorite 100 104
    FAVORITE_DOES_NOT_EXIST = "1001041000"

    # position
    POSITION_DOES_NOT_EXIST = "1001051000"
    POSITION_REPEAT = "1001051001"

    # trader
    BACK_TRADER_EXECUTE_ERROR = "1001061000"


exceptionMessage = ExceptionMessage()
